<?php session_start();?>
<?php

	$q = $_GET['id_resto'];
	$page =  isset($_GET['page']) ? $_GET['page'] : null ;
	
	if (!$q) return;
	
	set_include_path('../140dev/db');
	
	require_once('140dev_config.php');	  
	require_once('db_lib.php');
	
	$oDB = new db;
    
 	if(!$oDB)
  	{
  		die('Could not connect: ' . mysql_error());
  	}
	
	try{
		
		$sql = "select 'ATRAPALO' as CHANNEL, ID_RESTAURANT, DT_POST, NUM_SCORE, NUM_SCORE_CUISINE, ";
        $sql = $sql . "NUM_SCORE_AMBIENT, NUM_SCORE_VALUE_PRICE, NUM_SCORE_SERVICE, ";
        $sql = $sql . "ID_USER, NULL as DS_TITLE_POST, DS_POST ";
		$sql = $sql . "from HIST_POSTS_ATRAPALO ";
		$sql = $sql . "where ID_RESTAURANT = '" . $q . "' AND DS_STATUS = 'PROCESSED' ";
		$sql = $sql . "UNION ";
		$sql = $sql . "select 'SALIR' as CHANNEL, ID_RESTAURANT, DT_POST, NUM_SCORE * 2, NULL as NUM_SCORE_CUISINE, ";
        $sql = $sql . "NULL as NUM_SCORE_AMBIENT, NULL as NUM_SCORE_VALUE_PRICE, NULL as NUM_SCORE_SERVICE, ";
        $sql = $sql . "ID_USER, DS_TITLE_POST, DS_POST ";
		$sql = $sql . "from HIST_POSTS_SALIR ";
		$sql = $sql . "where ID_RESTAURANT = '" . $q . "' AND DS_STATUS = 'PROCESSED' ";
		$sql = $sql . "UNION ";
		$sql = $sql . "SELECT 'YELP' as CHANNEL, ID_RESTAURANT, DT_POST, NUM_SCORE*2, NULL as NUM_SCORE_CUISINE, ";
        $sql = $sql . "NULL as NUM_SCORE_AMBIENT, NULL as NUM_SCORE_VALUE_PRICE, NULL as NUM_SCORE_SERVICE, ";
        $sql = $sql . "ID_USER, NULL as DS_TITLE_POST, DS_POST ";
		$sql = $sql . "FROM HIST_POSTS_YELP ";
		$sql = $sql . "where ID_RESTAURANT = '" . $q . "' AND DS_STATUS = 'PROCESSED' ";
		$sql = $sql . "UNION ";
		$sql = $sql . "SELECT 'TRIPADVISOR' as CHANNEL, ID_RESTAURANT, DT_POST, NUM_SCORE*2, NUM_SCORE_CUISINE*2, ";
		$sql = $sql . "NUM_SCORE_AMBIENT*2, NUM_SCORE_VALUE_PRICE*2, NUM_SCORE_SERVICE*2, ";
        $sql = $sql . "ID_USER, DS_TITLE_POST, DS_POST ";
		$sql = $sql . "FROM HIST_POSTS_TRIPADVISOR ";
		$sql = $sql . "where ID_RESTAURANT = '" . $q . "' AND DS_STATUS = 'PROCESSED' ";
		$sql = $sql . "UNION ";
		$sql = $sql . "SELECT 'RESTALO' as CHANNEL, ID_RESTAURANT, DT_POST, NUM_SCORE, NUM_SCORE_CUISINE, ";
        $sql = $sql . "NUM_SCORE_AMBIENT, NUM_SCORE_VALUE_PRICE, NUM_SCORE_SERVICE, ";
        $sql = $sql . "ID_USER, NULL as DS_TITLE_POST, DS_POST ";
		$sql = $sql . "FROM HIST_POSTS_RESTALO ";
		$sql = $sql . "where ID_RESTAURANT = '" . $q . "' AND DS_STATUS = 'PROCESSED' ";
		$sql = $sql . "UNION ";
		$sql = $sql . "SELECT 'TENEDOR' as CHANNEL, ID_RESTAURANT, DT_POST, NUM_SCORE, NULL as NUM_SCORE_CUISINE, ";
		$sql = $sql . "NULL as NUM_SCORE_AMBIENT, NULL as NUM_SCORE_VALUE_PRICE, NULL as NUM_SCORE_SERVICE, ";
        $sql = $sql . "ID_USER, NULL as DS_TITLE_POST, DS_POST ";
		$sql = $sql . "FROM HIST_POSTS_TENEDOR ";
		$sql = $sql . "where ID_RESTAURANT = '" . $q . "' AND DS_STATUS = 'PROCESSED' ";
		$sql = $sql . "UNION ";
		$sql = $sql . "SELECT 'FOURSQUARE' as CHANNEL, T1.ID_RESTAURANT, DT_POST, NULL as NUM_SCORE, NULL as NUM_SCORE_CUISINE, ";
		$sql = $sql . "NULL as NUM_SCORE_AMBIENT, NULL as NUM_SCORE_VALUE_PRICE, NULL as NUM_SCORE_SERVICE, ";
        $sql = $sql . "T1.ID_USER, NULL as DS_TITLE_POST, DS_POST ";
		$sql = $sql . "FROM (HIST_POSTS_FOURSQUARE T1 INNER JOIN MAST_USERS_MODULES T2 ON T1.ID_RESTAURANT = T2.ID_RESTAURANT) "; 
		$sql = $sql . "INNER JOIN MAST_USERS T3 ON T2.ID_USER = T3.ID_USER ";
		$sql = $sql . "where T1.ID_RESTAURANT = '" . $q . "' AND T3.DS_USER_ID = '" . $_SESSION['username'] . "' AND T2.ID_MODULE = 10 ";
		$sql = $sql . "ORDER BY 3 DESC ";

		$result = $oDB->select($sql);
		
		$result_arr = array();		
		$opinion = array();
		while($row = mysqli_fetch_row($result)){
				$opinion['channel'] = $row[0];
				$opinion['id_resto'] = $row[1];
				$opinion['dt_post'] = date_create($row[2])->format('d/m/y');
				if(is_null($row[3]))
					$opinion['score'] = "";
				else
					$opinion['score'] = $row[3];
				if(is_null($row[4]))
					$opinion['score_cuisine'] = "";
				else
					$opinion['score_cuisine'] = $row[4];
				if(is_null($row[5]))
					$opinion['score_ambient'] = "";
				else
					$opinion['score_ambient'] = $row[5];
				if(is_null($row[6]))
					$opinion['score_price'] = "";
				else
					$opinion['score_price'] = $row[6];
				if(is_null($row[7]))
					$opinion['score_service'] = "";
				else
					$opinion['score_service'] = $row[7];
				$opinion['user'] = $row[8];
				if(is_null($row[9]))
					$opinion['ds_title_post'] = "";
				else
					$opinion['ds_title_post'] = $row[9];
				$opinion['ds_post'] = $row[10];
				array_push($result_arr,$opinion);	
		}
		
		
		print json_encode($result_arr, JSON_NUMERIC_CHECK);			
		
	}
	catch(Exception $e){
		echo $e;
	}	
	



?>